package com.mcxx.modules.minimumliving.controller;

import com.mcxx.modules.minimumliving.service.CustomFundPayService;
import com.mcxx.modules.minimumliving.service.FundPayService;
import com.mcxx.modules.base.utils.AdressUtil;
import com.mcxx.modules.miniSalvation.family.dto.CustomFundDTO;
import com.mcxx.modules.miniSalvation.family.param.SalvationObjectParam;
import com.mcxx.modules.miniSalvation.fundpay.dto.CustomFundPayMemberDTO;
import com.mcxx.modules.miniSalvation.fundpay.dto.FundPayMemberListDTO;
import com.mcxx.modules.miniSalvation.fundpay.param.CustomFundPayMemberParam;
import com.mcxx.modules.miniSalvation.fundpay.param.FundPayParam;
import com.mcxx.modules.miniSalvation.fundpay.service.read.CustomFundPayMemberReadService;
import com.mcxx.modules.miniSalvation.fundpay.service.read.FundPayMemberReadService;
import com.mcxx.service.DefaultRestService;
import com.mcxx.util.Constant;
import com.mcxx.util.DateUtil;
import com.mcxx.util.FileExportUtil;
import com.mcxx.util.ZipInfoUtil;
import com.yinhai.ta404.component.audit.vo.ExcelInfo;
import com.yinhai.ta404.core.restservice.annotation.RestService;
import com.yinhai.ta404.core.restservice.requestbean.PageParam;
import com.yinhai.ta404.core.restservice.resultbean.Page;
import com.yinhai.ta404.core.security.vo.UserAccountVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;


@Slf4j
@Validated
@Api("发放结果查询")
@RestService({"minimumliving/queryfundpay/v2"})
public class QueryFundPayController extends DefaultRestService {

    @Autowired
    private CustomFundPayMemberReadService customFundPayMemberReadService;
    @Autowired
    private FundPayMemberReadService fundPayMemberReadService;
    @Autowired
    private CustomFundPayService customFundPayService;
    @Autowired
    private FundPayService fundPayService;


    @PostMapping("getCustomFundPayDetails")
    @ApiOperation("自定义补发明细查询")
    public void getCustomFundPayDetails(CustomFundPayMemberParam customFundPayMemberParam, PageParam pageParam){
        log.info("自定义补发明细查询,参数customFundPayMemberParam：{}", customFundPayMemberParam);
        customFundPayMemberParam.setBusinessType(Constant.BusinessCode.DIBAO);
        UserAccountVo userAccountVo = getCurUserAccount();
        String areaCode = customFundPayMemberParam.getAreaCode();
        if(StringUtils.isEmpty(areaCode)){
            areaCode = userAccountVo.getAreaCode();
        }
        areaCode = AdressUtil.getAreaCode(areaCode);
        customFundPayMemberParam.setAreaCode(areaCode);
        Page page = customFundPayMemberReadService.getMemberByCustomFundPayIds(customFundPayMemberParam, pageParam);
        this.setPageBean(page);


        log.info("自定义补发明细查询,结果：{}", page);
    }


    @PostMapping("getCountFundPay")
    @ApiOperation("查看自定义补发名册统计信息")
    public void getCountFundPay(CustomFundPayMemberParam customFundPayMemberParam, PageParam pageParam){
        log.info("自定义补发明细查询--统计信息,参数customFundPayMemberParam：{}", customFundPayMemberParam);
        customFundPayMemberParam.setBusinessType(Constant.BusinessCode.DIBAO);
        CustomFundPayMemberDTO dto = customFundPayMemberReadService.getCountFundPay(customFundPayMemberParam);
        Integer households = dto.getHouseholds();
        dto = customFundPayMemberReadService.getCountFundPayMemberCount(customFundPayMemberParam);
        dto.setHouseholds(households);
        setData(dto);
        log.info("自定义补发明细查询--统计信息,结果：{}", dto);
    }


    @PostMapping("getCapitalFundPayFamilyLists")
    @ApiOperation("查询资金发放列表")
    public void getCapitalFundPayFamilyList(FundPayParam param, PageParam pageParam){
        log.info("查询资金发放明细,参数：{}", param);
        UserAccountVo userAccountVo = getCurUserAccount();
        String areaCode = param.getAreaCode();
        if(StringUtils.isEmpty(areaCode)){
            areaCode = userAccountVo.getAreaCode();
        }
        areaCode = AdressUtil.getAreaCode(areaCode);
        param.setAreaCode(areaCode);
        if(StringUtils.isNotEmpty(param.getPayDate()) &&  param.getPayDate().length()<9 ){
            param.setPayDate(param.getPayDate()+"-01");
        }
        Page page= fundPayService.querySelect(param,pageParam);
        setPageBean(page);
        log.info("查询资金发放明细,结果：{}", page);
    }


    @PostMapping("getFundPayMemberCount")
    @ApiOperation("统计列表信息")
    public void getFundPayMemberCount(FundPayParam param){
        log.info("统计资金发放名册数据,SalvationObjectParam：{}", param);
        UserAccountVo userAccountVo = getCurUserAccount();
        String areaCode = param.getAreaCode();
        if(StringUtils.isEmpty(areaCode)){
            areaCode = userAccountVo.getAreaCode();
        }
        areaCode = AdressUtil.getAreaCode(areaCode);
        param.setAreaCode(areaCode);

        if(StringUtils.isNotEmpty(param.getPayDate()) &&  param.getPayDate().length()<9 ){
            param.setPayDate(param.getPayDate() +"-01");
        }
        FundPayMemberListDTO dto = new FundPayMemberListDTO();
        if(StringUtils.isNotEmpty(param.getIdcard()) || StringUtils.isNotEmpty(param.getName()) ){
            dto.setCountFamily("0");
            dto.setCountMember("0");
            dto.setSumNumber("0");
        }else{
            dto = fundPayMemberReadService.getFundPayMemberCount(param);
        }
        this.setData(dto);
        log.info("统计资金发放名册数据,结果：{}", dto);
    }


    @PostMapping("getCustomFundPayFamilyList")
    @ApiOperation("自定义补发查询")
    public void getCustomFundPayFamilyList(SalvationObjectParam param, PageParam pageParam){
        log.info("自定义补发查询,SalvationObjectParam：{}, PageParam:{}", param, pageParam);

        if(StringUtils.isEmpty(param.getSendMonth())){
            param.setSendMonth(DateUtil.getMonthString());
        }
        List<CustomFundDTO> customFundDTOS = customFundPayService.queryCustomByAllMem(param, getCurUserAccount());
        setData(customFundDTOS);
        log.info("自定义补发查询,结果：{}", customFundDTOS);
    }


    @PostMapping({"downLoadFundPayMemberList"})
    public void downLoadFundPayMemberList(HttpServletResponse response,@RequestBody FundPayParam param) throws IOException {
        ExcelInfo excelInfo = fundPayService.exportExcelFundPayMember(param.getExportType(),param.getIdsList(),param,getCurUserAccount());
        if(null != excelInfo){
            FileExportUtil.exportFileResponse(response, excelInfo, getCurUserAccount().getAreaName());
//            ZipInfoUtil.exportZipResponse(response, excelInfo, this.getCurUserId());
        }
    }


    @PostMapping({"downLoadCustFundPayMemberList"})
    public void downLoadCustFundPayMemberList(HttpServletResponse response,@RequestBody SalvationObjectParam param) throws IOException {
        if(StringUtils.isEmpty(param.getSendMonth())){
            param.setSendMonth(DateUtil.getMonthString());
        }
        ExcelInfo excelInfo = customFundPayService.exportExcelFundPayMember(param.getExportType(),param.getMemberIdList(),param,getCurUserAccount());
        if(null != excelInfo){
            ZipInfoUtil.exportZipResponse(response, excelInfo, this.getCurUserId());
        }
    }
}
